// admin/notification-center/notification-center.js
// const db = wx.cloud.database(); // <-- ***删除：不再直接访问数据库***

Page({
    data: {
      notifications: [],
      loading: true,
      showAddDialog: false,
      newNotification: {
        title: '',
        content: ''
      }
    },
  
    onShow: function () {
      this.getNotifications();
    },
  
    // --- *** 修改：使用云函数获取列表 *** ---
    getNotifications: function () {
      this.setData({ loading: true });
      wx.cloud.callFunction({
        name: 'managenotifications',
        data: {
          action: 'getList'
        }
      }).then(res => {
        if (res.result.success) {
          res.result.data.forEach(item => {
            item.timeFormatted = new Date(item.time).toLocaleString();
          });
          this.setData({
            notifications: res.result.data,
            loading: false
          });
        } else {
          this.setData({ loading: false });
          wx.showToast({ title: res.result.message || '加载失败', icon: 'none' });
        }
      }).catch(err => {
        this.setData({ loading: false });
        wx.showToast({ title: '加载失败', icon: 'none' });
      });
    },
  
    showAddDialog: function () {
      this.setData({ showAddDialog: true });
    },
  
    closeDialog: function () {
      this.setData({
        showAddDialog: false,
        newNotification: { title: '', content: '' } // 重置输入
      });
    },
    
    onInputDialog: function(e) {
      const { field } = e.currentTarget.dataset;
      this.setData({
          [`newNotification.${field}`]: e.detail.value
      });
    },
  
    // --- *** 修改：使用云函数发布新通知 *** ---
    onAddNotification: function () {
      const { title, content } = this.data.newNotification;
      if (!title || !content) {
        wx.showToast({ title: '标题和内容不能为空', icon: 'none' });
        return;
      }
  
      wx.showLoading({ title: '正在发布...' });
  
      wx.cloud.callFunction({
        name: 'managenotifications',
        data: {
          action: 'add',
          data: {
            title: title,
            content: content
          }
        }
      }).then((res) => {
        wx.hideLoading();
        if (res.result.errMsg && res.result.errMsg.includes('ok')) {
          wx.showToast({ title: '发布成功' });
          this.closeDialog();
          this.getNotifications();
        } else {
          wx.showToast({ title: res.result.message || '发布失败', icon: 'none' });
        }
      }).catch(() => {
        wx.hideLoading();
        wx.showToast({ title: '发布失败', icon: 'none' });
      });
    },
  
    // --- *** 修改：使用云函数删除通知 *** ---
    deleteNotification: function(e) {
      const notificationId = e.currentTarget.dataset.id;
      wx.showModal({
        title: '确认删除',
        content: '确定要删除这条通知吗？此操作不可逆。',
        success: res => {
          if (res.confirm) {
            wx.showLoading({ title: '删除中...' });
            wx.cloud.callFunction({
              name: 'managenotifications',
              data: {
                action: 'delete',
                notificationId: notificationId
              }
            }).then((res) => {
              wx.hideLoading();
              if (res.result.success) {
                wx.showToast({ title: '删除成功' });
                this.getNotifications();
              } else {
                wx.showToast({ title: res.result.message || '删除失败', icon: 'none' });
              }
            }).catch(() => {
              wx.hideLoading();
              wx.showToast({ title: '删除失败', icon: 'none' });
            });
          }
        }
      });
    }
  });